// Subject 接口
interface Subject {
    void attach(Observer o);
    void detach(Observer o);
    void notify();
}

// Observer 接口
interface Observer {
    void update();
}

// ConcreteSubject 类
class ConcreteSubject implements Subject {
    private List<Observer> observers = new ArrayList<>();

    @Override
    public void attach(Observer o) {
        observers.add(o);
    }

    @Override
    public void detach(Observer o) {
        observers.remove(o);
    }

    @Override
    public void notify() {
        for (Observer observer : observers) {
            observer.update();
        }
    }

    public void operation() {
        System.out.println("ConcreteSubject operation");
        notify();
    }
}

// ConcreteObserverA 类
class ConcreteObserverA implements Observer {
    @Override
    public void update() {
        System.out.println("ConcreteObserverA update");
    }
}

// ConcreteObserverB 类
class ConcreteObserverB implements Observer {
    @Override
    public void update() {
        System.out.println("ConcreteObserverB update");
    }
}

// 测试类
public class ObserverTest {
    public static void main(String[] args) {
        ConcreteSubject subject = new ConcreteSubject();
        Observer observerA = new ConcreteObserverA();
        Observer observerB = new ConcreteObserverB();

        subject.attach(observerA);
        subject.attach(observerB);

        subject.operation(); // 输出：ConcreteSubject operation
                             //        ConcreteObserverA update
                             //        ConcreteObserverB update

        subject.detach(observerA);
        subject.notify(); // 输出：ConcreteObserverB update
    }
}